package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"web-tools-go/utils"
)

func RandPage(c *gin.Context) {
	c.HTML(http.StatusOK, "rand.html", gin.H{})
}

func GenerateRand(c *gin.Context) {
	l, _ := strconv.Atoi(c.PostForm("len"))
	types := c.PostFormArray("types[]")
	s := ""
	for _, v := range types {
		s += v
	}
	if l == 0 || len(types) == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"msg":  "",
		})
		return
	}
	resp := utils.GetRandByStringAndLength(s, l)
	c.JSON(http.StatusOK, gin.H{
		"code": 1,
		"msg":  resp,
	})
}
